import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import './assets/css/global.css'
import axios from 'axios'

axios.defaults.baseURL = 'https://cnodejs.org/api/v1'
Vue.prototype.$http = axios
Vue.config.productionTip = false

Vue.filter('dateFormat', function(originVal1) {
  // var originVal = originVal.replace('T', ' ')
  // var originVal1 = originVal.replace('R', '')
  const dt = new Date(originVal1)
  const now = new Date()
  //计算时间间隔，单位为分钟
  var inter = parseInt((now.getTime() - dt.getTime()) / 1000 / 60)

  function formatTime(inter) {
    var timeNames = [
      [1, '刚刚'],
      [60, '分钟前'],
      [60 * 24, '小时前'],
      [60 * 24 * 30, '天前'],
      [60 * 24 * 30 * 12, '个月前'],
      [Number.MAX_VALUE, '年前']
    ]
    // if(inter < timeNames[0][0]) {
    //   return timeNames[0][1]
    // }
    let item = ''
    for (var i = 0; i <= timeNames.length; i++) {
      const base = timeNames[i][0]
      if (inter < base) {
        return item + timeNames[i][1]
      }
      item = parseInt(inter / base).toString()
    }
  }
  return formatTime(inter)

  /* if (inter == 0) {
    return '刚刚'
  }
  //多少分钟前
  else if (inter < 60) {
    return inter.toString() + '分钟前'
  }
  //多少小时前
  else if (inter < 60 * 24) {
    return parseInt(inter / 60).toString() + '小时前'
  }
  //多少天前
  else if (inter < 60 * 24 * 30) {
    return parseInt(inter / 60 / 24).toString() + '天前'
  }
  //多少月前
  else if (inter < 60 * 24 * 30 * 12) {
    return parseInt(inter / 60 / 24 / 30).toString() + '个月前'
  } else {
    return parseInt(inter / 60 / 24 / 30 / 12).toString() + '年前'
  } */
  /* const y = dt.getFullYear()
  const m = (dt.getMonth() + 1 + '').padStart(2, '0')
  const d = (dt.getDate() + '').padStart(2, '0')
  const hh = (dt.getHours() + '').padStart(2, '0')
  const mm = (dt.getMinutes() + '').padStart(2, '0')
  const ss = (dt.getSeconds() + '').padStart(2, '0')
  return `${y}-${m}-${d} ${hh}:${mm}:${ss}` */
})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
